const path = require('path')

const config = {
  // 项目名称
  projectName: 'yanshop',
  // 项目创建日期
  date: '2019-11-12',
  // 设计稿尺寸
  designWidth: 750,
  deviceRatio: {
    '640': 2.34 / 2,
    '750': 1,
    '828': 1.81 / 2
  },
  // 项目源码目录
  sourceRoot: 'src',
  // 项目产出目录
  outputRoot: `dist/${process.env.TARO_ENV}`,
  // 通用插件配置
  plugins: {
    babel: {
      sourceMap: true,
      presets: [
        ['env', {
          modules: false
        }]
      ],
      plugins: [
        'transform-decorators-legacy',
        'transform-class-properties',
        'transform-object-rest-spread'
      ]
    },
    // ts
    typescript: {
      compilerOptions: {
        allowSyntheticDefaultImports: true,
        baseUrl: '.',
        declaration: false,
        experimentalDecorators: true,
        jsx: 'react',
        jsxFactory: 'Nerv.createElement',
        module: 'commonjs',
        moduleResolution: 'node',
        noImplicitAny: false,
        noUnusedLocals: true,
        outDir: './dist/',
        preserveConstEnums: true,
        removeComments: false,
        rootDir: '.',
        sourceMap: true,
        strictNullChecks: true,
        target: 'es6'
      },
      include: [
        'src/**/*'
      ],
      exclude: [
        'node_modules'
      ],
      compileOnSave: false
    },
    // 压缩
    uglify: {
      enable: true,
      config: {
      // 配置项同 https://github.com/mishoo/UglifyJS2#minify-options
      }
    },
    // css压缩
    csso: {
      enable: true,
      config: {
        // 配置项同 https://github.com/css/csso#minifysource-options
      }
    }
  },
  // 全局变量设置
  defineConstants: {
  },
  // 路径别名
  alias: {
    '@/': path.resolve(__dirname, '..', 'src/')
  },
  // 文件 copy 配置
  copy: {
    patterns: [
    ],
    options: {
    }
  },
  // 小程序端专用配置
  weapp: {
    module: {
      postcss: {
        autoprefixer: {
          enable: true,
          config: {
            browsers: [
              'last 3 versions',
              'Android >= 4.1',
              'ios >= 8'
            ]
          }
        },
        pxtransform: {
          enable: true,
          config: {
          }
        },
        url: {
          enable: true,
          config: {
            limit: 10240 // 设定转换尺寸上限
          }
        },
        cssModules: {
          enable: false, // 默认为 false，如需使用 css modules 功能，则设为 true
          config: {
            namingPattern: 'module', // 转换模式，取值为 global/module
            generateScopedName: '[name]__[local]___[hash:base64:5]'
          }
        }
      }
    },
    compile: {
      compressTemplate: true  // wxml压缩
    },
    // 替换 JSX 中的属性名
    jsxAttributeNameReplace: {}
  },
  // H5 端专用配置
  h5: {
    devServer: {
      port: 10086
    },
    publicPath: '/',
    staticDirectory: 'static',
    module: {
      postcss: {
        autoprefixer: {
          enable: true,
          config: {
            browsers: [
              'last 3 versions',
              'Android >= 4.1',
              'ios >= 8'
            ]
          }
        },
        cssModules: {
          enable: false, // 默认为 false，如需使用 css modules 功能，则设为 true
          config: {
            namingPattern: 'module', // 转换模式，取值为 global/module
            generateScopedName: '[name]__[local]___[hash:base64:5]'
          }
        }
      }
    },
    // 输出配置
    output: {
      filename: 'js/[name].[hash:8].js',
      chunkFilename: 'js/[name].[chunkhash:8].js'
    },
    imageUrlLoaderOption: {
      limit: 5000,
      name: 'static/images/[name].[hash].[ext]'
    },
    miniCssExtractPluginOption: {
      filename: 'css/[name].[hash].css',
      chunkFilename: 'css/[name].[chunkhash].css',
    },
    // 自定义 Webpack 配置
    webpackChain: {},
    devServer: {},
    // 额外插件
    esnextModules: ['taro-ui']
  }
}

module.exports = function (merge) {
  switch (process.env.NODE_ENV) {
    case 'development':
      return merge({}, config, require('./dev'))
    case 'production':
      return merge({}, config, require('./prod'))
    case 'devbH5':
      return merge({}, config, require('./devbH5'))
    case 'probH5':
        return merge({}, config, require('./probH5'))
    default:
      return merge({}, config, require('./dev'))
  }
}
